package com.adlx.dingdong.mall.core;

import com.adlx.dingdong.mall.client.ProductCategoryClient;
import com.adlx.dingdong.mall.domain.ShopGoodsCategory;
import com.adlx.dingdong.mall.domain.ShopGoodsCategoryCriteria;
import com.adlx.dingdong.mall.dto.ShopGoodsCategoryDTO;
import com.adlx.dingdong.mall.mapper.ShopGoodsCategoryMapper;
import com.adlx.dingdong.mall.vo.ShopGoodsCategoryVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.adlx.dingdong.mall.commons.utils.Beans.batchTransform;

/**
 * Created by dulei on 18/1/9.
 */
@RefreshScope
@RestController
public class ProductCategoryClientImpl implements ProductCategoryClient {
    @Autowired
    private ShopGoodsCategoryMapper shopGoodsCategoryMapper;

    @RequestMapping(value = "/productCategory", method = RequestMethod.POST)
    public PageInfo getList(@RequestBody ShopGoodsCategoryDTO categoryDTO) {
        if (null == categoryDTO) {
            categoryDTO = new ShopGoodsCategoryDTO();
        }
        PageHelper.startPage(categoryDTO.getPageNum(), categoryDTO.getPageSize());
        ShopGoodsCategoryCriteria criteria = new ShopGoodsCategoryCriteria();
        ShopGoodsCategoryCriteria.Criteria categoryCriteria = criteria.createCriteria();
        categoryCriteria.andIsenabledEqualTo(new Byte("1"));
        categoryCriteria.andDepthEqualTo(1);
        categoryCriteria.andIsdeletedEqualTo(new Byte("0"));
        if (null != categoryDTO.getParentId()) {
            categoryCriteria.andParentidEqualTo(categoryDTO.getParentId());
        }


        criteria.setOrderByClause("OrderBy asc");
        List<ShopGoodsCategory> categoryList = shopGoodsCategoryMapper.selectByExample(criteria);
        return new PageInfo(batchTransform(ShopGoodsCategoryVO.class, categoryList));
    }


}
